How to Develop ERC20 Token Faucet

如何开发一个ERC20代币水龙头

Posted by Mr. Alex on 2023-08-12
Estimated Reading Time 1 Minutes
Words 294 In Total
Viewed Times

Overview | 业务场景:

一个项目在测试阶段要经过市场和团队内部的多频次测试,要用到代币,为了不是每次都是转账给一个新的账户
通过实现代币水龙头的功能来快捷的实现领取测试token。

具体判断的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract XERC20 is ERC20, ERC20Burnable, Pausable, Ownable {

uint256 public amountAllowed = 100; // Number of claims per time
mapping(address => bool) requestAddress; // received

event MintFaucetToken(address _receive,uint256 _amount);

constructor() ERC20("XMax", "XMX") {}

function pause() public onlyOwner {
_pause();
}

function unpause() public onlyOwner {
_unpause();
}

function mint(address to, uint256 amount) public {
_mint(to, amount);
}

function _beforeTokenTransfer(address from, address to, uint256 amount)
internal
whenNotPaused
override
{
super._beforeTokenTransfer(from, to, amount);
}

//水龙头功能 ,每次请求直接去Mint就好,如果需要添加世间和地址限制,根据需求在此处添加就好
function requestTokens() external {
mint(msg.sender,amountAllowed * 10**18);
emit MintFaucetToken(msg.sender,amountAllowed * 10**18);

}
}
通过以上,就可以简单实现ERC20代币水龙头功能,仅在测试网用!!!

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !